﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core.DBUtility;
using Micrhard.FMCG.Server.DataAccess;
using Micrhard.FMCG.Server.DTO;
using Microhard.Core;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.Models;

namespace Micrhard.FMCG.Server.Impl
{
    public class ProductDisplayImageImpl : IProductDisplayImage
    {
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.ProductDisplayImageModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.ProductDisplayImages.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ProductDisplayImageMsg.AddRecFailed);
                Log.Error("IProductDisplayImage.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.ProductDisplayImageModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.ProductDisplayImages.Where(l => ids.Contains(l.ImageID));
            foreach (var e in entity)
            {
                db.ProductDisplayImages.DeleteObject(e);
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, ProductDisplayImageMsg.DeleteFailed);
                Log.Error("IProduct.Delete", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.ProductDisplayImageModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ProductDisplayImageModel GetModelByID(Guid id)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.ProductDisplayImages.SingleOrDefault(w => w.ImageID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public Models.ProductDisplayImageModel GetModelByModel(Models.ProductDisplayImageModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ProductDisplayImageModel> Query(Models.ProductDisplayImageModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            IEnumerable<ProductDisplayImage> entities = from b in db.ProductDisplayImages
                                                        where b.SubKACode == Model.SubKACode
                                                        select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
            {
                var rnt = pagedEntities.Select(m => m.ToModel()).ToList();
                var emp = db.Employees.SingleOrDefault(d => d.UserName == this.scope.CurrentUserName);
                //节省传输资源
                rnt.ForEach(i =>
                {
                    i.Conetnt = null;
                    var now = i.CreateTime.Date;
                    var end = now.AddDays(1);
                    var ck = db.EmployeeCheckIns.SingleOrDefault(c => c.CreateTime >= now && c.CreateTime <= end && c.SubKACode == i.SubKACode);
                    if (ck != null)
                    {
                        //赋予陈列码
                        i.PrdDispKACode = ck.SubKACode;
                    }
                    if (emp.Range != (int)Range.Manager)
                    {
                        i.CanDeleteOrUpdate = true;
                    }
                });
                return rnt;
            }
            return new List<ProductDisplayImageModel>();
        }

        public List<Models.ProductDisplayImageModel> Query(Models.ProductDisplayImageModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
